﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Smarly.Bussines;
using Smarly.Bussines.Membership;
using Smarly.Bussines.Model.Account;
using Smarly.Web.Controllers.Base;
using Smarly.Web.Helpers;

namespace Smarly.Web.Controllers
{
	public class AccountController : BaseController
	{
		#region SignIn

		[HttpPost]
		public ActionResult Index(Login model)
		{
			model.Put();

			if (ModelState.IsValid)
				Response.LogIn(Request, model.Email, model.HashCode);

			if (ModelState.IsValid && Request.Cookies["authref"] != null)
			{
				var c = Request.Cookies["authref"];
				Response.Cookies.Add(new HttpCookie("authref", string.Empty) { Path = "/", Domain = Request.AuthDomain(), Expires = new DateTime(1900, 1, 1) });
				Response.Cookies.Add(new HttpCookie("authurlref", string.Empty) { Path = "/", Domain = Request.AuthDomain(), Expires = new DateTime(1900, 1, 1) });
				return Json(new {redirect = c.Value});
			}
			else
			{return JsonValidationErrors();}
		}

		#endregion

		#region Create User

		[HttpPost]
		public ActionResult Create(NewAccount model)
		{
			model.Post();

			if (ModelState.IsValid)
				Response.LogIn(Request, model.Email, model.HashCode);

			if (ModelState.IsValid && Request.Cookies["authref"] != null)
			{
				var c = Request.Cookies["authref"];
				Response.Cookies.Add(new HttpCookie("authref", string.Empty) { Path = "/", Domain = Request.AuthDomain(), Expires = new DateTime(1900, 1, 1) });
				Response.Cookies.Add(new HttpCookie("authurlref", string.Empty) { Path = "/", Domain = Request.AuthDomain(), Expires = new DateTime(1900, 1, 1) });
				return Json(new { redirect = c.Value });
			}
			else
			{ return JsonValidationErrors(); }
		}

		#endregion

		#region Repair Password

		[HttpPost]
		public ActionResult Repear(RepearPassword model)
		{
			var uri = new Uri(Request.FullDomain() + Url.Action("UpdatePassword", "Account", new { hashcode = "HASHCODE" }));
			model.Url = Request.UrlReferrer.OriginalString.UpdateUrlByParam(
				"repair",
				/*HttpUtility.UrlEncode(uri.LocalPath + HttpUtility.UrlDecode(uri.Query))*/"HASHCODE"
				);

			model.Get();

			return JsonValidationErrors();
		}

		#endregion

		#region Update Password

		[HttpPost]
		public ActionResult UpdatePassword(UpdatePassword model)
		{
			model.Put();

			if (ModelState.IsValid)
				Response.LogIn(Request, model.Email, model.HashCode);

			if (ModelState.IsValid && Request.Cookies["authref"] != null)
			{
				var c = Request.Cookies["authref"];
				Response.Cookies.Add(new HttpCookie("authref", string.Empty) { Path = "/", Domain = Request.AuthDomain(), Expires = new DateTime(1900, 1, 1) });
				Response.Cookies.Add(new HttpCookie("authurlref", string.Empty) { Path = "/", Domain = Request.AuthDomain(), Expires = new DateTime(1900, 1, 1) });
				return Json(new { redirect = c.Value });
			}
			else
			{ return JsonValidationErrors(); }
		}

		#endregion

		#region SignOut

		public ActionResult Logout()
		{
			Response.LogOut(Request);
			if (Request.UrlReferrer != null && !string.IsNullOrWhiteSpace(Request.UrlReferrer.ToString()))
				return Redirect(Request.UrlReferrer.ToString());
			return RedirectToAction("Index", "Home");
		}

		#endregion

		#region Profile

		public ActionResult UserInfo(string emailsh1)
		{
			return View();
		}

		#endregion

	}
}
